home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zcolor2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.6 KB  |  210 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zcolor2.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Level 2 color operators */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "gscolor.h"
  24. #include "gscssub.h"
  25. #include "gsmatrix.h"
  26. #include "gsstruct.h"
  27. #include "gxcspace.h"
  28. #include "gxfixed.h"        /* for gxcolor2.h */
  29. #include "gxcolor2.h"
  30. #include "gxdcolor.h"        /* for gxpcolor.h */
  31. #include "gxdevice.h"
  32. #include "gxdevmem.h"        /* for gxpcolor.h */
  33. #include "gxpcolor.h"
  34. #include "estack.h"
  35. #include "ialloc.h"
  36. #include "istruct.h"
  37. #include "idict.h"
  38. #include "idparam.h"
  39. #include "igstate.h"
  40. #include "store.h"
  41.  
  42. /* Forward references */
  43. private int store_color_params(P3(os_ptr, const gs_paint_color *,
  44.                   const gs_color_space *));
  45. private int load_color_params(P3(os_ptr, gs_paint_color *,
  46.                  const gs_color_space *));
  47.  
  48. /* Test whether a Pattern instance uses a base space. */
  49. inline private bool
  50. pattern_instance_uses_base_space(const gs_pattern_instance_t *pinst)
  51. {
  52.     return pinst->type->procs.uses_base_space(pinst->type->procs.get_pattern(pinst));
  53. }
  54.  
  55. /* - currentcolor <param1> ... <paramN> */
  56. private int
  57. zcurrentcolor(i_ctx_t *i_ctx_p)
  58. {
  59.     os_ptr op = osp;
  60.     const gs_client_color *pc = gs_currentcolor(igs);
  61.     const gs_color_space *pcs = gs_currentcolorspace(igs);
  62.     int n;
  63.  
  64.     check_ostack(5);        /* Worst case: CMYK + pattern */
  65.     if (pcs->type->index == gs_color_space_index_Pattern) {
  66.     gs_pattern_instance_t *pinst = pc->pattern;
  67.  
  68.     n = 1;
  69.     if (pinst != 0 && pattern_instance_uses_base_space(pinst))    /* uncolored */
  70.         n += store_color_params(op, &pc->paint,
  71.            (const gs_color_space *)&pcs->params.pattern.base_space);
  72.     op[n] = istate->pattern;
  73.     } else
  74.     n = store_color_params(op, &pc->paint, pcs);
  75.     push(n);
  76.     return 0;
  77. }
  78.  
  79. /* - .currentcolorspace <array|int> */
  80. private int
  81. zcurrentcolorspace(i_ctx_t *i_ctx_p)
  82. {
  83.     os_ptr op = osp;
  84.  
  85.     push(1);
  86.     *op = istate->colorspace.array;
  87.     if (r_has_type(op, t_null)) {
  88.     /*
  89.      * Return the color space index.  This is only possible
  90.      * for the parameterless color spaces.
  91.      */
  92.     gs_color_space_index csi = gs_currentcolorspace_index(igs);
  93.  
  94.     make_int(op, (int)(csi));
  95.     }
  96.     return 0;
  97. }
  98.  
  99. /* <param1> ... <paramN> setcolor - */
  100. private int
  101. zsetcolor(i_ctx_t *i_ctx_p)
  102. {
  103.     os_ptr op = osp;
  104.     gs_client_color c;
  105.     const gs_color_space *pcs = gs_currentcolorspace(igs);
  106.     int n, code;
  107.     gs_pattern_instance_t *pinst = 0;
  108.  
  109.     if (pcs->type->index == gs_color_space_index_Pattern) {
  110.     /* Make sure *op is a real Pattern. */
  111.     ref *pImpl;
  112.  
  113.     if (r_has_type(op, t_null)) {
  114.         c.pattern = 0;
  115.         n = 1;
  116.     } else {
  117.         check_type(*op, t_dictionary);
  118.         check_dict_read(*op);
  119.         /*
  120.          * We have no way to check for a subclass of st_pattern_instance,
  121.          * so just make sure the structure is large enough.
  122.          */
  123.         if (dict_find_string(op, "Implementation", &pImpl) <= 0 ||
  124.         !r_is_struct(pImpl) ||
  125.         gs_object_size(imemory, r_ptr(pImpl, const void)) <
  126.         sizeof(gs_pattern_instance_t)
  127.         )
  128.         return_error(e_rangecheck);
  129.         pinst = r_ptr(pImpl, gs_pattern_instance_t);
  130.         c.pattern = pinst;
  131.         if (pattern_instance_uses_base_space(pinst)) {    /* uncolored */
  132.         if (!pcs->params.pattern.has_base_space)
  133.             return_error(e_rangecheck);
  134.         n = load_color_params(op - 1, &c.paint,
  135.                       (const gs_color_space *)&pcs->params.pattern.base_space);
  136.         if (n < 0)
  137.             return n;
  138.         n++;
  139.         } else
  140.         n = 1;
  141.     }
  142.     } else {
  143.     n = load_color_params(op, &c.paint, pcs);
  144.     c.pattern = 0;        /* for GC */
  145.     }
  146.     if (n < 0)
  147.     return n;
  148.     code = gs_setcolor(igs, &c);
  149.     if (code < 0)
  150.     return code;
  151.     if (pinst != 0)
  152.     istate->pattern = *op;
  153.     pop(n);
  154.     return code;
  155. }
  156.  
  157. /* <array> .setcolorspace - */
  158. private int
  159. zsetcolorspace(i_ctx_t *i_ctx_p)
  160. {
  161.     os_ptr op = osp;
  162.  
  163.     check_type(*op, t_array);
  164.     istate->colorspace.array = *op;
  165.     pop(1);
  166.     return 0;
  167. }
  168.  
  169. /* ------ Initialization procedure ------ */
  170.  
  171. const op_def zcolor2_l2_op_defs[] =
  172. {
  173.     op_def_begin_level2(),
  174.     {"0currentcolor", zcurrentcolor},
  175.     {"0.currentcolorspace", zcurrentcolorspace},
  176.     {"1setcolor", zsetcolor},
  177.     {"1.setcolorspace", zsetcolorspace},
  178.     op_def_end(0)
  179. };
  180.  
  181. /* ------ Internal procedures ------ */
  182.  
  183. /* Store non-pattern color values on the operand stack. */
  184. /* Return the number of values stored. */
  185. private int
  186. store_color_params(os_ptr op, const gs_paint_color * pc,
  187.            const gs_color_space * pcs)
  188. {
  189.     int n = cs_num_components(pcs);
  190.  
  191.     if (pcs->type->index == gs_color_space_index_Indexed)
  192.     make_int(op + 1, (int)pc->values[0]);
  193.     else
  194.     make_floats(op + 1, pc->values, n);
  195.     return n;
  196. }
  197.  
  198. /* Load non-pattern color values from the operand stack. */
  199. /* Return the number of values stored. */
  200. private int
  201. load_color_params(os_ptr op, gs_paint_color * pc, const gs_color_space * pcs)
  202. {
  203.     int n = cs_num_components(pcs);
  204.     int code = float_params(op, n, pc->values);
  205.  
  206.     if (code < 0)
  207.     return code;
  208.     return n;
  209. }
  210.